package wicketforge.converter;

import org.jdom.Element;
import org.jetbrains.annotations.NotNull;

import com.intellij.openapi.diagnostic.Logger;

public class PathConverterFactory {

	private static final Logger LOG = Logger.getInstance("#wicketforge.converter.PathConverterFactory");

	@NotNull
	public static PathConverter readPathConverter(final Element child) {
		final String type = child.getAttributeValue(PathConverter.TYPE);
		try {
			Class<? extends PathConverter> clazz = (Class<? extends PathConverter>) Class.forName(type);
			PathConverter pathConverter = clazz.newInstance();
			pathConverter.readFromConfiguration(child);
			return pathConverter;
		} catch (ClassNotFoundException e) {
			LOG.error("Unable to instantiate class '" + type + "'", e);
			throw new RuntimeException(e);
		} catch (InstantiationException e) {
			LOG.error("Unable to instantiate class '" + type + "'", e);
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			LOG.error("Unable to instantiate class '" + type + "'", e);
			throw new RuntimeException(e);
		}
	}

	public static void writePathConverter(final PathConverter converter, final Element child) {
		converter.writeToConfiguration(child);
	}
}
